package com.woniuxy.controller;

import com.woniuxy.ResponseResult;
import com.woniuxy.entity.Perms;
import com.woniuxy.entity.RolePerms;
import com.woniuxy.service.PermService;
import com.woniuxy.service.RolePermsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 权限系统的crud
 */
@RestController
@RequestMapping("/perms")
public class RolePermsController {
    @Autowired
    PermService permService;

    @Autowired
    RolePermsService rolePermsService;

    /**
     * 展示所有权限
     * @return
     */
    @PostMapping("/showperms")
    public ResponseResult<List<Perms>> showPerms(){
        List<Perms> list = permService.list();
        ResponseResult<List<Perms>> responseResult = new ResponseResult<>();
        responseResult.setMsg("展示成功");
        responseResult.setCode(200);
        responseResult.setData(list);
        return responseResult;
    }

    /**
     * 传入角色id和权限id来分配权限
     */
    @PostMapping("/addrolePerm")
    public ResponseResult<Void> rolePerms(int rid,int pid) {
        RolePerms rolePerms = new RolePerms();
        rolePerms.setRid(rid);
        rolePerms.setPid(pid);
        boolean save = rolePermsService.save(rolePerms);
        if (save){
            return ResponseResult.SUCCESS;
        }else {
            return ResponseResult.ERR;
        }
    }
}
